<template>
  <div class="addbox">
    <div class="boxHeader">
      <p>{{title}}</p>
      <i class="el-icon-close" @click="closeAddBox"></i>
    </div>
    <div class="addMainBox">
      <el-form
        :model="sensorInfoDTO"
        :rules="rules"
        ref="statisticalItemsDTO"
        label-width="168px"
        class="demo-ruleForm"
      >
        <el-form-item label="传感器:"  class="need_name" prop="deviceTypeName">
          <el-select v-model="sensorInfoDTO.deviceTypeName" placeholder="请选择" @change="selectChange" :disabled="isEdit">
            <el-option
              v-for="item in selectList"
              :key="item.deviceTypeId"
              :value="item.deviceTypeName"
              >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="传感器ID:"  class="need_name" prop="sensorId">
          <el-input type="text" placeholder="请输入传感器ID：" maxlength="10"  v-model="sensorInfoDTO.sensorId" :disabled="isEdit"></el-input>
        </el-form-item>
        <el-form-item label="位置:"  class="need_name" prop="location">
          <el-input type="text" placeholder="请输入位置：" maxlength="10"  v-model="sensorInfoDTO.location" ></el-input>
        </el-form-item>
        <el-form-item label="预警值:"  class="name">
          <el-input type="text" placeholder="请输入预警值："  v-model="sensorInfoDTO.sensorWarn" ></el-input>
        </el-form-item>
        <el-form-item label="告警值:"  class="name">
          <el-input type="text" placeholder="请输入告警值："  v-model="sensorInfoDTO.sensorAlarm" ></el-input>
        </el-form-item>
      </el-form>
    </div>
    <footer>
      <div class="cancel flex-center" @click="closeAddBox">取消</div>
      <div class="determainAdd flex-center" @click="determainAdd">确定</div>
    </footer>
  </div>
</template>

<script>
export default {
  props: {
    selectList: {
      require: Array
    }
  },
  data () {
    return {
      title: '添加传感器',
      sensorInfoDTO: {
        deviceTypeName: '',
        sensorId: '',
        location: '',
        sensorWarn: '',
        sensorAlarm: '',
        isEdit: false
      },
      rules: {
        deviceTypeName: [
          { required: true, message: '请选择', trigger: 'blur' }
        ],
        sensorId: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        location: [
          { required: true, message: '请输入', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    deviceData () {
      let data = {}
      if (this.$store.state.sceneController.deviceData) {
        data = this.$store.state.sceneController.deviceData
      }
      return data
    },
    isEdit () {
      let flag = false
      if (this.deviceData.sensorId) {
        flag = true
      }
      return flag
    },
    projectId () {
      return localStorage.getItem('projectId')
    }
  },
  mounted () {
    // console.log(this.deviceData)
    if (this.deviceData.sensorId) {
      this.title = '编辑传感器'
      this.sensorInfoDTO = {
        deviceTypeName: this.deviceData.deviceTypeName,
        sensorId: this.deviceData.sensorId,
        location: this.deviceData.location,
        sensorWarn: this.deviceData.sensorWarn,
        sensorAlarm: this.deviceData.sensorAlarm
      }
    }
  },
  methods: {
    closeAddBox () {
      // console.log('close')
      this.$store.commit('set_ifShowCover', false)
    },
    determainAdd () {
      this.$refs.statisticalItemsDTO.validate((valid) => {
        if (valid) {
          const currentList = this.$store.state.sceneController.currentList
          const deviceTypeId = this.selectList.filter(item => item.deviceTypeName === this.sensorInfoDTO.deviceTypeName)[0].deviceTypeId
          const params = {
            ...this.sensorInfoDTO,
            deviceTypeId,
            power: 60,
            projectId: this.projectId,
            sceneId: currentList.sceneId,
            sceneTypeId: 2,
            threshold: '1111'
          }
          if (this.deviceData.sensorId) {
            const sensorId = this.deviceData.sensorId
            // console.log(sensorId)
            this.$store.dispatch('sceneController/UpdateDevice', { params, sensorId }).then(res => {
              if (res.data.code === 200) {
                this.closeAddBox()
                this.$store.commit('sceneController/update_deviceData', null)
                this.$store.dispatch('sceneController/updateTableData', {
                  projectId: this.projectId,
                  index: 1,
                  pageSize: 10,
                  sceneId: currentList.sceneId
                })
              } else {
                this.$message({
                  message: res.data.message,
                  type: 'error',
                  duration: 1000
                }
                )
              }
            })
            return
          }
          this.$store.dispatch('sceneController/addDevice', params).then(res => {
            if (res.data.code === 200) {
              this.closeAddBox()
              this.$store.dispatch('sceneController/updateTableData', {
                projectId: this.projectId,
                index: 1,
                pageSize: 10,
                sceneId: currentList.sceneId
              })
            } else {
              this.$message({
                type: 'error',
                message: res.data.message,
                duration: 1000
              })
            }
          })
        }
      })
    },
    selectChange () {
    }
  }
}
</script>

<style scoped lang="scss">
.addbox {
  position: fixed;
  z-index: 1000;
  top: 10%;
  width: 840px;
  height: 720px;
  background: rgba(255, 255, 255, 1);
  left: 50%;
  transform: translateX(-50%);
    .boxHeader {
    height: 72px;
    border-bottom: solid 1px #e6e6e6;
    p {
      float: left;
      line-height: 72px;
      font-size: 18px;
      font-weight: 500;
      color: rgba(51, 51, 51, 1);
      padding-left: 20px;
    }
    .el-icon-close {
      float: right;
      margin-right: 23px;
      cursor: pointer;
      line-height: 72px;
      font-size: 24px;
    }
  }
  .addMainBox {
    position: relative;
    height: 534px;
    border-bottom: solid 1px #e6e6e6;
    padding: 42px 0 0 42px;
      ::v-deep .el-form-item{
        height: 48px;
        line-height: 48px;
        margin-bottom: 32px;
      }
      ::v-deep .el-form-item__label {
        width: 168px !important;
        text-align: right;
        font-size: 16px;
        color: #333333;
      }
    .need_name {
      ::v-deep .el-form-item__label:before {
        content: "*";
        color: #4868ff;
      }
      .el-input {
        position: absolute;
        left: 168px;
        ::v-deep .el-input__inner {
          width: 400px;
          height: 48px;
          border-radius: 6px;
          border: 1px solid rgba(220, 220, 220, 1);
          font-size: 16px;
        }
      }
      ::v-deep .el-form-item__error {
        position: absolute;
        left: 175px;
        top: 55px;
        width: 400px;
      }
    }
      .name {
      .el-input {
        position: absolute;
        left: 168px;
        ::v-deep .el-input__inner {
          width: 400px;
          height: 48px;
          border-radius: 6px;
          border: 1px solid rgba(220, 220, 220, 1);
          font-size: 16px;
        }
      }
      ::v-deep .el-form-item__error {
        position: absolute;
        left: 175px;
        top: 50px;
        width: 400px;
      }
    }
      .select {
        position: absolute;
        width: 168px;
        height: 22px;
        font-size: 16px;
        font-weight: 400;
        line-height: 22px;
        text-align: right;
        top: 53px;
        left: 42px;
      }
      .select::before {
        content: "*";
        color: #4868ff;
      }
      .el-select {
        width: 400px;
        height: 48px;
       ::v-deep .el-input--suffix .el-input__inner{
          height: 48px;
          line-height: 48px;
        }
      }
  }
    footer {
    height: 112px;
    position: relative;
    .cancel {
      cursor: pointer;
      position: absolute;
      top: 32px;
      left: 280px;
      width: 128px;
      height: 48px;
      border-radius: 28px;
      border: 2px solid rgba(72, 104, 255, 1);
      font-size: 18px;
      font-weight: 500;
      color: rgba(72, 104, 255, 1);
    }
    .determainAdd {
      cursor: pointer;
      position: absolute;
      top: 32px;
      right: 280px;
      width: 128px;
      height: 48px;
      background: rgba(72, 104, 255, 1);
      border-radius: 28px;
      font-size: 18px;
      font-weight: 500;
      color: rgba(255, 255, 255, 1);
    }
  }
}
</style>
<style>
 .el-select-dropdown__item.hover, .el-select-dropdown__item:hover {
  background:rgba(87,131,255,0.1);
}
.el-select-dropdown__item.selected {
    background:rgba(87,131,255,1);
    color: #fff;
    font-weight: 400;
}
</style>
